﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Input;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq;
using Common.Library;
using Common;
 #if NOTANDROID
using Windows.Storage;
using Windows.Storage.Streams;
#endif
#if WP8 && NOTANDROID
using System.ServiceModel.Syndication;
#endif
using System.Threading.Tasks;
#if (WP8 && NOTANDROID) || ANDROID
using System.IO.IsolatedStorage;
#endif
#if WINDOWS_8
using Windows.Web.Syndication;
#endif
namespace OnlineVideos.Library
{

    public class ShowDownloadForBlogs
    {
		#if ANDROID
		System.Collections.ObjectModel.ObservableCollection<FeedItem> currentFeed = new System.Collections.ObjectModel.ObservableCollection<FeedItem> ();
		#endif
        public static List<DateTimeOffset> PublishedDate = new List<DateTimeOffset>();
        public static System.Threading.AutoResetEvent auto = new System.Threading.AutoResetEvent(false);
        public void StartDownload()
        {
            if (AppSettings.BlogStatus == 0)
            {
                if (!String.IsNullOrEmpty(AppSettings.Secondaryshowsfromblog))
                {
                    if (Task.Run(async () => await Storage.FileExists("Movies.xml")).Result)
                    Storage.DeleteFile("Movies.xml");
                    ReadRss(new Uri(AppSettings.Secondaryshowsfromblog, UriKind.Absolute));
# if WP8 || ANDROID
                    auto.WaitOne();
#endif
                    AppSettings.BlogStatus = 1;
                }
                else
                {
                    if (Task.Run(async () => await Storage.FileExists("Movies.xml")).Result)
                    Storage.DeleteFile("Movies.xml");
                    AppSettings.BlogStatus = 1;
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(AppSettings.Primaryshowsfromblog))
                {
                    ReadRss(new Uri(AppSettings.Primaryshowsfromblog, UriKind.Absolute));
# if WP8 || ANDROID
                    auto.WaitOne();
#endif
                }
            }
        }
        public async void ReadRss(Uri rssUri)
        {
            XDocument xdoc = new XDocument();
            string ttitle = string.Empty;

            if (Task.Run(async () => await Storage.FileExists("Movies.xml")).Result)
            {
             #if ( WINDOWS_8 && NOTANDROID)

                StorageFolder store = ApplicationData.Current.LocalFolder;
                StorageFile file = Task.Run(async () => await store.CreateFileAsync(@"Movies.xml", Windows.Storage.CreationCollisionOption.OpenIfExists)).Result;
                var f = Task.Run(async () => await file.OpenAsync(FileAccessMode.Read)).Result;
                IInputStream inputStream = f.GetInputStreamAt(0);
                DataReader dataReader = new DataReader(inputStream);
                uint numBytesLoaded = Task.Run(async () => await dataReader.LoadAsync((uint)f.Size)).Result;
                string xmlData = (dataReader.ReadString(numBytesLoaded)).ToString();
                System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(xmlData));
                xdoc = XDocument.Load(ms);
                ttitle = xdoc.ToString();
                dataReader.DetachStream();
                dataReader.Dispose();
                inputStream.Dispose();
#endif
#if ANDROID || WP8
				IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
                 IsolatedStorageFileStream fs = isoStore.OpenFile("Movies.xml", FileMode.Open, FileAccess.Read);
                    xdoc = XDocument.Load(fs);
                    ttitle = xdoc.ToString();
                    fs.Close();
                    fs.Dispose();
                    isoStore.Dispose();
#endif
            }
            else
            {
                ttitle = string.Empty;
            }
#if( WINDOWS_8 && NOTANDROID)
                SyndicationClient client = new SyndicationClient();
                client.BypassCacheOnRetrieve = true;
			   var currentFeed = Task.Run(async () => await client.RetrieveFeedAsync(rssUri)).Result.Items;
#endif
#if  ANDROID || WP8
            HttpWebRequest request = (HttpWebRequest)
                WebRequest.Create(rssUri);
			request.Method = HttpMethod.Get;
            HttpWebResponse response1 = (HttpWebResponse) Task.Run(async()=> await request.GetResponseAsync()).Result;
            Stream str = response1.GetResponseStream(); //e.Result;
			#if ANDROID
		var	currentFeed= Task.Run(async () => await  SyndicationFeed.Load(str)).Result;	

			#endif
			#if NOTANDROID
			XmlReader response = XmlReader.Create(str);
			var currentFeed = SyndicationFeed.Load(response).Items;
			#endif

#endif



			if (currentFeed == null)
                return;
            string stringtomatch = "<NewDataSet>";

			foreach (var f in currentFeed)
            {
				#if (WP8 && NOTANDROID) ||( WINDOWS_8 && NOTANDROID)
				string SummaryText=f.Summary.Text;
				#endif
				#if ANDROID
				string SummaryText=f.Description;
				#endif
                   #if ( WINDOWS_8 && NOTANDROID)
                if ((DateTimeOffset)f.PublishedDate.DateTime > AppSettings.LastUpdatedDate)
                #endif
#if ( WP8 && NOTANDROID) || ANDROID
                if ((DateTimeOffset)f.PublishDate.Date > AppSettings.LastUpdatedDate.Date)
#endif
                {
                    if (ttitle == "")

						ttitle = WebUtility.HtmlDecode(Regex.Replace(SummaryText, "<[^>]+?>", ""));
                    else
						ttitle = ttitle.Substring(0, ttitle.LastIndexOf('<')) + (WebUtility.HtmlDecode(Regex.Replace(SummaryText, "<[^>]+?>", ""))).Substring(stringtomatch.Length);

                }
                   #if ( WINDOWS_8 && NOTANDROID)
                PublishedDate.Add((DateTimeOffset)f.PublishedDate.DateTime);
#endif
                 #if ( WP8 && NOTANDROID)
                PublishedDate.Add((DateTimeOffset)f.PublishDate.DateTime);
#endif
				#if ANDROID
				PublishedDate.Add((DateTimeOffset)f.PublishDate);
				#endif   
            }


            if (ttitle != "")
            {
#if ( WINDOWS_8 && NOTANDROID)
                StorageFolder store1 = ApplicationData.Current.LocalFolder;
                StorageFile file1 = Task.Run(async () => await store1.CreateFileAsync(@"Movies.xml", Windows.Storage.CreationCollisionOption.ReplaceExisting)).Result;
                var fquery1 = Task.Run(async () => await file1.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite)).Result;
                var outputStream =  fquery1.GetOutputStreamAt(0);
                var writer = new Windows.Storage.Streams.DataWriter(outputStream);
                writer.WriteString(ttitle);
                var fi = Task.Run(async () => await writer.StoreAsync()).Result;
                var oi = Task.Run(async () => await outputStream.FlushAsync()).Result;
                writer.DetachStream();
                outputStream.Dispose();
                fquery1.Dispose();
#endif
#if ANDROID || WP8
                        StreamWriter writer = null;
                        IsolatedStorageFile isoStore1 = IsolatedStorageFile.GetUserStoreForApplication();
                        try
                        {
                            if (Task.Run(async()=> await Storage.FileExists("Movies.xml")).Result)
                                writer = new StreamWriter(new IsolatedStorageFileStream("Movies.xml", FileMode.Open, isoStore1));
                            else
                                writer = new StreamWriter(new IsolatedStorageFileStream("Movies.xml", FileMode.Create, isoStore1));
                            writer.Write(ttitle);
                            writer.Close();
                            writer.Dispose();
                            isoStore1.Dispose();
                        }
                        catch (Exception ex)
                        {
                            Exceptions.SaveOrSendExceptions("Exception in ReadRss Method In ShowDownloadFromBlog.cs file", ex);
                        }
#endif
                if (AppSettings.BlogStatus == 1)
                {
                    if (PublishedDate.Count > 0)
                        AppSettings.LastPublishedDate = (from p in PublishedDate orderby p descending select p).FirstOrDefault();
                    PublishedDate.Clear();
                    AppSettings.BackgroundAgentStatus = SyncAgentStatus.UpdateShows;
                    AppSettings.BlogStatus = 0;
                }
                else
                {
                    AppSettings.BlogStatus = 1;
                }

# if WP8 || ANDROID
                auto.Set();
#endif
            }
            else
            {
                if (AppSettings.BlogStatus == 1)
                {
                    AppSettings.BackgroundAgentStatus = SyncAgentStatus.UpdateShows;

                }
                if (AppSettings.BlogStatus == 1)
                {
                    AppSettings.BlogStatus = 0;
                }
                else
                {
                    AppSettings.BlogStatus = 1;
                }
# if WP8 || ANDROID
                auto.Set();
#endif
            }

        }

    }


}
